/**
 * SaveFPBiz.java
 *2014年4月22日下午8:49:36
 *LINKT
 */
package com.bh.biz;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

import com.bh.db.dao.TfpmxCfDao;
import com.bh.db.dao.TfpmxDao;
import com.bh.db.entry.TFpmx;
import com.bh.db.entry.TFpxm;

/**
 * @author LINKT
 *
 */
public class SaveFPBiz {

        
        public int  insert(Document doc) {
                String sn=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
                TFpmx fpmx=new TFpmx();//发票明细
                TFpxm fpxm=null;//发票项目
                List<TFpxm> list=new ArrayList<TFpxm>();
                TfpmxDao dao=new TfpmxDao();
                sn+=dao.getSN();
                
                
                Element root=doc.getRootElement();
                fpmx.setFpdm(root.elementText("fpdm"));
                fpmx.setFphm(root.elementText("fphm"));
                fpmx.setJqbh(root.elementText("jqbh"));
                fpmx.setSkdw(root.elementText("skdw"));
                fpmx.setSh(root.elementText("sh"));
                fpmx.setKprq(root.elementText("kprq"));
                fpmx.setSky(root.elementText("sky"));
                fpmx.setFkdw(root.elementText("fkdw"));
                fpmx.setKhbh(root.elementText("khbh"));
                fpmx.setHjdl(root.elementText("hjdl"));
                fpmx.setHjdf(root.elementText("hjdf"));
                fpmx.setXxhj(root.elementText("xxhj"));
                fpmx.setDxhj(root.elementText("dxhj"));
                //发票状态
                fpmx.setKplx("00");
                fpmx.setSfjx(BigDecimal.valueOf(0));
                fpmx.setFpzt(BigDecimal.valueOf(Long.parseLong(root.elementText("fpzt"))));
                fpmx.setCreatetime(sn);
                
                List<Element> fpxmList=root.element("xmmx").elements("data");
                if (fpxmList.size()>0) {
                        int i=1;
                        for (Element element : fpxmList) {
                                if (fpxm==null) {
                                        fpxm=new TFpxm();
                                }
                                fpxm.setFpdm(root.elementText("fpdm"));
                                fpxm.setFphm(root.elementText("fphm"));
                                fpxm.setXmname(element.attributeValue("name"));
                                fpxm.setXmvalue(element.attributeValue("value"));
                                fpxm.setXmsort(BigDecimal.valueOf(i));
                                fpxm.setCreatetime(sn);
                                list.add(fpxm);
                                i++;
                                fpxm=null;
                        }
                }
                //检查发票是否重复
                TFpmx fpmx2=dao.getFpmxByDmHm(fpmx);
                if (fpmx2==null) {
                        dao.addFpmx(fpmx);
                        for (TFpxm e : list) {
                                dao.addFpxm(e);
                        }
                        dao.commit();
                        dao.close();
                        return 1;//1成功
                }else {
                        TfpmxCfDao dao2=new TfpmxCfDao();
                        dao2.addFpmx(fpmx);
                        for (TFpxm e1 : list) {
                                dao2.addFpxm(e1);
                        }
                        dao2.commit();
                        dao2.close();
                        dao.close();
                        return 0;//0重复
                }

        }
        
        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub

        }

}
